package com.colin.app.fragment.permission;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.app.AppFragment;
import com.colin.library.help.LogHelp;
import com.colin.library.help.permission.PermissionHelp;

import java.util.List;


public class PermissionFragment extends AppFragment implements PermissionContract.PermissionView, View.OnClickListener {
    private PermissionContract.PermissionPresenter mPermissionPresenter = null;

    private static final String FRAGMENT_ID = "fragment_id";
    private int fragment_id;

    public static PermissionFragment newInstance(AppActivity mAppActivity, int fragment_id) {
        activity = mAppActivity;
        PermissionFragment fragment = new PermissionFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(FRAGMENT_ID, fragment_id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        fragment_id = bundle.getInt(FRAGMENT_ID);
    }

    @Override
    public void onDestroyView() {
        if (null!=mPermissionPresenter){
            mPermissionPresenter.destroy();
            mPermissionPresenter = null;
        }
        super.onDestroyView();
    }

    @Override
    public void initPresenter() {
        mPermissionPresenter = new PermissionPresenterImpl((AppActivity) activity,this);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.fragment_permission;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
        this.getView().findViewById(R.id.button_permission_camera).setOnClickListener(this);
        this.getView().findViewById(R.id.button_permission_contacts).setOnClickListener(this);
        this.getView().findViewById(R.id.button_permission_storage).setOnClickListener(this);
        this.getView().findViewById(R.id.button_permission_more).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_permission_camera:
                mPermissionPresenter.checkPermissionCamera();
                break;
            case R.id.button_permission_contacts:
                mPermissionPresenter.checkPermissionContacts();
                break;
            case R.id.button_permission_storage:
                mPermissionPresenter.checkPermissionStorage();
                break;
            case R.id.button_permission_more:
                mPermissionPresenter.checkPermissionMore();
                break;
        }
    }

    @Override
    public boolean checkPermission(String permission) {
        return PermissionHelp.getInstance().checkPermission(this, permission);
    }

    @Override
    public boolean checkPermission(List<String> permissionList) {
        return PermissionHelp.getInstance().checkPermission(this, permissionList);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        LogHelp.e("requestCode==" + String.valueOf(requestCode));
        if (grantResults.length < 1) {
            LogHelp.e("权限请求失败");
        }
        for (String permission : permissions) {
            LogHelp.e("权限请求=permissions=" + permission);
        }
        for (int result : grantResults) {
            LogHelp.e("权限请求=result=" + String.valueOf(result));
        }
        //判断是否申请成功
        if (PermissionHelp.getInstance().onRequestPermissionsResult(grantResults)){
            if (requestCode == PermissionHelp.getInstance().REQUEST_CODE_CAMERA){
                mPermissionPresenter.checkPermissionCamera();
            }else   if (requestCode == PermissionHelp.getInstance().REQUEST_CODE_CONTACTS){
                mPermissionPresenter.checkPermissionContacts();
            }else   if (requestCode == PermissionHelp.getInstance().REQUEST_CODE_STORAGE){
                mPermissionPresenter.checkPermissionStorage();
            }else   if (requestCode == PermissionHelp.getInstance().REQUEST_CODE_MORE){
                mPermissionPresenter.checkPermissionMore();
            }
        }else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
